local util = require "util"
local math_floor                      = math.floor
local math_random                    = math.random
local M = {}

-- 牌的类型
CARD_SUIT = {
	NONE = 0,	--大小王 ■
	CLUB =	1,  -- 梅 ♣
	DIAMOND = 2,  -- 方 ♦
	HEART =	3,  -- 红 ♥
	SPADE =	4,  -- 黑 ♠
}

-- 面值特征字符 （X ：10 V ：小王 W ：大王）
CARD_RANK_CHAR = { 'A','2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K', 'V', 'W'}

--花色
function card_to_suit(card)
	return math_floor(card / 100) 
end

--花色转换字符
function suit_to_str(suit)
	local s = "■"
	if suit == CARD_SUIT.DIAMOND then
		s = "♦"
	elseif suit == CARD_SUIT.CLUB then
		s = "♣"
	elseif suit == CARD_SUIT.HEART then
		s = "♥"
	elseif suit == CARD_SUIT.SPADE then
		s = "♠"
	end
	return s
end

--扑克转换字符
function card_to_str(card)
	if not card then
		return "nil"
	end
	local suit = card_to_suit(card)
	local rank = CARD_RANK_CHAR[card%100]
	return suit_to_str(suit) .. rank
end

-- 洗牌
function shuffle(cards)
    local card_count = #cards
    swap_times = card_count or swap_times

    for i = 1, swap_times do
        local n = math_random(1, card_count)
        local m = math_random(1, card_count)

        if n ~= m then
            cards[n], cards[m] = cards[m], cards[n]
        end
    end
end


--扑克组合转换字符
function cards_to_str(cards)
	local str = ""
	if type(cards) ~= "table" then return str end
	for _,v in ipairs(cards) do
		str = str .. card_to_str(v) .. ","
	end
	return str
end

-- 牌桌转换str 
-- {
-- uid = { side = 1 , cars = {1,2,3,4,5}}
-- 
--}
--
function card_list_to_str(list)
	local str = ""
	for i,v in pairs(list) do
		str = str .." uid:".. i .. " side:" .. v.side .. " cards:" .. cards_to_str(v.cards)
	end
	return str
end

-- 牌桌转换str 
-- {
-- [1] = {1,2,3,4,5}}
-- 
--}
--
function card_list_to_str_kp(list)
	local str = ""
	for i,v in ipairs(list) do
		str = str .. " i:" .. i .. " cards:" .. cards_to_str(v)
	end
	return str
end

-- 牌桌转换str 
-- {
-- uid = {1,2,3,4,5}}
-- 
--}
--
function card_list_to_str_zjh(list)
	local str = ""
	for i,v in pairs(list) do
		str = str .. " uid:" .. i .. " cards:" .. cards_to_str(v)
	end
	return str
end

-- 牌桌转换str 
-- {
-- 101 : ture
-- 
--}
--
function card_list_to_str_ddz(list)
	local str = ""
	for i,v in pairs(list) do
		str = str .. " uid:" .. i .. " cards:" .. cards_to_str_ddz(v).."\n"
	end
	return str
end

--扑克组合转换字符
function cards_to_str_ddz(cards)
	local str = ""
	if type(cards) ~= "table" then return str end
	for i,v in pairs(cards) do
		str = str .. card_to_str(i) .. ","
	end
	return str
end


M.cards_to_str = cards_to_str		--扑克组合转换字符
M.card_list_to_str = card_list_to_str  --扑克组集合转换字符
M.shuffle = shuffle                 --洗牌




M.card_list_to_str_kp = card_list_to_str_kp
M.card_list_to_str_zjh = card_list_to_str_zjh
M.card_list_to_str_ddz = card_list_to_str_ddz
return M